# -*- coding: utf-8 -*-
import re,urllib,os,json
import sys
import threading

stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr
reload(sys)
sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stde
print sys.stdout
sys.setdefaultencoding('utf-8')
import logging
# 日志功能

# 配置日志信息  
logging.basicConfig(level=logging.INFO,  
                    format='%(asctime)s %(message)s',  
                    datefmt='%Y-%m-%d %H:%M:%S',  
                    filename='mz.log' ,  
                    filemode='a')  
# 定义一个Handler打印INFO及以上级别的日志到sys.stderr  
console = logging.StreamHandler()  
console.setLevel(logging.INFO)  
# 设置日志打印格式  
formatter = logging.Formatter('[%(asctime)s] %(message)s')  
console.setFormatter(formatter)  
# 将定义好的console日志handler添加到root logger  
logging.getLogger('').addHandler(console)
def namecheck(str0):
    str0 = str0.strip()
    # 文件名是不允许/ \ 等的，所以会出错
    str0 = str0.replace('/','／')
    str0 = str0.replace('\\','＼')
    str0 = str0.replace('*','＊')
    str0 = str0.replace('?','？')
    str0 = str0.replace('"','＂')
    str0 = str0.replace('<','＜')
    str0 = str0.replace('>','＞')
    str0 = str0.replace(':','：')
    str0 = str0.replace('|','｜')
    return str0
def getCourseInfo(courseid):
    url = 'http://www.maiziedu.com/course/%s' %courseid
    reg = r'<li><a href="(.*?)" target="_blank" class="font14 color66"><span class="fl">(.*?)</span><span class="fr color99">(.*?)</span></a></li>'
    reg2 = r'<h1 class="color33 font24 marginB10">(.*?)</h1>'
    html = urllib.urlopen(url).read().decode('utf-8')
    videolist = re.findall(reg,html)
    try:
        title = re.findall(reg2,html)[0]
    except:
        logging.error("该课程【%s】不存在，估计被删了，跳过" %courseid)
        return
    else:
        title = namecheck(title)
        title = '[%s]%s' %(courseid,title)
        logging.info('《%s》' %title)
        if title != None:
            if not os.path.exists('%s' %title):
                os.mkdir('%s' %title)
        else:
            logging.error('标题获取出错')
            return    
        if videolist != None:
            readlist(title,videolist)
            # 爬不到地址 先不做
            #(docname , docurl) = getDocURL(courseid)
            #downloadDoc(title,docurl,docname)
            #writellist(title,videolist)
        else:
            logging.error('列表获取出错')
            return
        
def writellist(title,videolist):
    data = '{"data":{['
    data1 = ''
    filepath = '%s/list.txt' %title
    print filepath
    #filepath = filepath.decode('utf-8')
    fn = open(filepath ,'wb')
    for i in videolist:
        if data1 == '':
            data1 = '{"url":"%s","title":"%s","during":"%s"}' %(i[0],i[1],i[2])
        else:
            data1 = '%s,{"url":"%s","title":"%s","during":"%s"}' %(data1,i[0],i[1],i[2])
    data = '%s%s]}}' %(data,data1)
    print data
    data = bytes(data)
    fn.write(data)
    fn.close
def getVideoURL(videopage):
    url = 'http://www.maiziedu.com%s' %(videopage)
    # 加入.mp4通配 防止视频链接故障时意外中断。
    reg = r'<source src="(.*?\.mp4)" type=\'video/mp4\' />'
    html = urllib.urlopen(url).read().decode('utf-8')
    try:
        videourl = re.findall(reg,html)[0]
    except:
        videourl = ''
    # print '9%s9' %videourl
    # if videourl == '':
    #    print '123'
    return videourl
def readlist(title,videolist):
    for i in videolist:
        url = getVideoURL(i[0]);
        if url == '':
            logging.info("【错误】%s 视频链接有误，已跳过。" %title)
            continue
        else:
            downloadVideo(title,url,i[1])
        
def downloadVideo(title,url,name):
    # logging.info('%s开始下载。。。' %name)
    name = namecheck(name)
    courseid0 = int(re.findall(r'\[(\d+)\]',title)[0])
    if os.path.exists('%s/%s.mp4' %(title,name)):
        logging.info('[%d]文件存在，跳过' %courseid0)
        return
    else:
        # 对url进行转码，防止中文时出错
        url = url.encode('utf-8')
        # print url
        filename = r'%s/%s.mp4' %(title,name)
        urllib.urlretrieve(url,filename)
        #video = urllib.urlopen(url.encode('utf-8')).read()
        #fn = open('%s/%s.mp4' %(title,name),'wb')
        #fn.write(video)
        #fn.close
        logging.info('[%d]%s下载完成。' %(courseid0,name))
def getDocURL(courseid):
    url = 'http://www.maiziedu.com/course/%s' %courseid
    html = urllib.urlopen(url).read().decode('utf-8')
    reg = r'<div class="col_l">.*?<span class="font14 color66">(.*?)</span>'
    reg2 = r'lesson_download(.*?)</li>'
    docname = re.findall(reg,html)
    docurl = re.findall(reg2,html,re.S)
    print docurl
    for i in docname:
        i = 1
    docurl = 'http://www.maiziedu.com%s' %docurl
    return (docname,docurl)
def downloadDoc(title,url,name):
    filetype = re.findall(r'http://.*?/.*?\.(.*?)$',url)[0]
    if os.path.exists('%s/%s.%s' %(title,name,filetype)):
        logging.info('文件存在，跳过')
        return;
    else:
        doc = urllib.urlopen(url).read();
        fn = open('%s/%s.%s' %(title,name,filetype),'wb')
        fn.write(doc)
        fn.close
        logging.info('%s.%s下载完成。' %(name,filetype))
def begin(start,end):
    # 已完成 1-32
    a = (705,231,774,624,641,316,334,18,770,809,768,811,727)
    # 不存在 24
    # 最新 882
    for i in range(start,end+1):
        # print i
        if i not in a:
            logging.info("[%s / %s]" %(i ,end))
            getCourseInfo(i)
    # 爬不到地址 先不做
    # getDocURL(705)
    logging.info('[%s / %s] 本线程任务已结束' %(i ,end))
    
# 多线程
threads = []
t1 = threading.Thread(target=begin, args=(811,820,))
threads.append(t1)
#t2 = threading.Thread(target=begin, args=(726,750,))
#threads.append(t2)
#t3 = threading.Thread(target=begin, args=(845,850,))
#threads.append(t3)
t4 = threading.Thread(target=begin, args=(871,890,))
threads.append(t4)
t5 = threading.Thread(target=begin, args=(631,650,))
threads.append(t5)
for t in threads:
    t.setDaemon(True)
    t.start()
